package com.ruoyi.asms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.ams.controller.AmsAssetTypeController;
import com.ruoyi.asms.domain.AsmsFaultAsset;
import com.ruoyi.asms.domain.vo.AssetRepairNumberVo;
import com.ruoyi.asms.domain.vo.AssetTypeFaultNumberVo;
import com.ruoyi.asms.mapper.AsmsAssetFullInfoMapper;
import com.ruoyi.common.core.domain.AsmsAssetFullInfo;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * 资产售后维护详细信息Service业务层处理
 * 
 * @author yfkj
 * @date 2021-10-28
 */
@Service
public class AsmsAssetFullInfoServiceImpl extends ServiceImpl<AsmsAssetFullInfoMapper, AsmsAssetFullInfo>  implements IAsmsAssetFullInfoService
{
    @Autowired
    private AsmsAssetFullInfoMapper asmsAssetFullInfoMapper;

    @Autowired
    private IAsmsFaultAssetService asmsFaultAssetService;

    @Autowired
    AmsAssetTypeController remoteAssetTypeService;



    /**
     * 查询资产售后维护详细信息
     * 
     * @param id 资产售后维护详细信息主键
     * @return 资产售后维护详细信息
     */
    @Override
    public AsmsAssetFullInfo selectAsmsAssetFullInfoById(Long id)
    {
        return asmsAssetFullInfoMapper.selectAsmsAssetFullInfoById(id);
    }

    /**
     * 查询资产售后维护详细信息列表
     * 
     * @param asmsAssetFullInfo 资产售后维护详细信息
     * @return 资产售后维护详细信息
     */
    @Override
    public List<AsmsAssetFullInfo> selectAsmsAssetFullInfoList(AsmsAssetFullInfo asmsAssetFullInfo)
    {
        return asmsAssetFullInfoMapper.selectAsmsAssetFullInfoList(asmsAssetFullInfo);
    }

    /**
     * 新增资产售后维护详细信息
     * 
     * @param asmsAssetFullInfo 资产售后维护详细信息
     * @return 结果
     */
    @Override
    public int insertAsmsAssetFullInfo(AsmsAssetFullInfo asmsAssetFullInfo)
    {
        asmsAssetFullInfo.setCreateTime(System.currentTimeMillis());
        asmsAssetFullInfo.setCreateBy(SecurityUtils.getUsername());
        return asmsAssetFullInfoMapper.insertAsmsAssetFullInfo(asmsAssetFullInfo);
    }

    /**
     * 修改资产售后维护详细信息
     * 
     * @param asmsAssetFullInfo 资产售后维护详细信息
     * @return 结果
     */
    @Override
    public int updateAsmsAssetFullInfo(AsmsAssetFullInfo asmsAssetFullInfo)
    {
        asmsAssetFullInfo.setUpdateTime(System.currentTimeMillis());
        asmsAssetFullInfo.setUpdateBy(SecurityUtils.getUsername());

        return asmsAssetFullInfoMapper.updateAsmsAssetFullInfo(asmsAssetFullInfo);
    }

    /**
     * 批量删除资产售后维护详细信息
     * 
     * @param ids 需要删除的资产售后维护详细信息主键
     * @return 结果
     */
    @Override
    public int deleteAsmsAssetFullInfoByIds(Long[] ids)
    {
        return asmsAssetFullInfoMapper.deleteAsmsAssetFullInfoByIds(ids);
    }

    /**
     * 删除资产售后维护详细信息信息
     * 
     * @param id 资产售后维护详细信息主键
     * @return 结果
     */
    @Override
    public int deleteAsmsAssetFullInfoById(Long id)
    {
        return asmsAssetFullInfoMapper.deleteAsmsAssetFullInfoById(id);
    }


    @Override
    public AssetTypeFaultNumberVo getFaultNumberByAssetType(TreeSelect tree) {
        Long assetTypeId = tree.getKey();
       /** Long deptId =SecurityUtils.getDeptId(); */
        Long deptId = 27l;

        //获取所有子类型故障信息
        List<AsmsFaultAsset> assetTypeFaultInfos = asmsFaultAssetService.
                list(new QueryWrapper<AsmsFaultAsset>().like("asset_type_ancestors", assetTypeId).eq("use_dept_id",deptId));

        //获取本类型故障信息
        List<AsmsFaultAsset> typeInfos = asmsFaultAssetService.
                list(new QueryWrapper<AsmsFaultAsset>().eq("asset_type_id",assetTypeId).eq("use_dept_id",deptId));

        AssetTypeFaultNumberVo vo =new AssetTypeFaultNumberVo();
        vo.setKey(assetTypeId);
        //如果本类型故障记录没有
        if(typeInfos == null || typeInfos.size() < 1){
            if(assetTypeId == 0l){
                vo.setTitle("0");
            }else{
                //远程获取类型名称
                vo.setTitle(remoteAssetTypeService.getInfo(assetTypeId).getData().getAssetTypeName());
            }
            vo.setNumber(0);
        }else{
            AsmsAssetFullInfo asmsAssetFullInfo = asmsAssetFullInfoMapper.selectOne(new QueryWrapper<AsmsAssetFullInfo>().
                    eq("asset_type_id", assetTypeId).groupBy("asset_type_id"));
            vo.setTitle(asmsAssetFullInfo.getAssetTypeName());
            vo.setNumber(typeInfos.size());
        }

        //如果没有下一级子类型信息
        if(tree.getChildren() == null || tree.getChildren().size() <1){
            vo.setChildren(null);
            return vo;
        }else{
            List<AssetTypeFaultNumberVo> childrens = new LinkedList<>();

            for(TreeSelect treeSelect : tree.getChildren()){
                childrens.add(getFaultNumberByAssetType(treeSelect));
            }
            vo.setChildren(childrens);
        }

        vo.setNumber(vo.getNumber()+assetTypeFaultInfos.size());

        return vo;
    }

    @Override
    public AssetRepairNumberVo getRepairResultByDept(TreeSelect tree) {
        List<AsmsFaultAsset> maintenanceList = null;
        Integer repairNumber = 0;
        if(tree != null && tree.getKey() != null){
             maintenanceList = asmsFaultAssetService.list(new QueryWrapper<AsmsFaultAsset>().eq("is_finish", 1).
                    eq("maintenance_dept_id", tree.getKey()));
        }else{
            return null;
        }

        if(maintenanceList != null){
            repairNumber = maintenanceList.size();
        }

        AssetRepairNumberVo vo =new AssetRepairNumberVo();

        vo.setKey(tree.getKey());
        vo.setTitle(tree.getTitle());
        vo.setRepairNumber(repairNumber);
        vo.setRepairSumNumber(repairNumber);
        List<TreeSelect> childrens = tree.getChildren();
        if(childrens == null || childrens.size() < 1){
            vo.setChildren(null);
            return vo;
        }else{
            List<AssetRepairNumberVo> vos = new LinkedList<>();
            for(TreeSelect t : childrens){
                AssetRepairNumberVo tmpVo = getRepairResultByDept(t);
                vos.add(tmpVo);
                vo.setRepairSumNumber(vo.getRepairSumNumber() + tmpVo.getRepairSumNumber());
            }
            vo.setChildren(vos);
        }

        return vo;
    }
}
